package com.junchenrobot.mes.core;

import com.junchenrobot.mes.client.KCClient;
import com.junchenrobot.mes.dao.DeviceDao;
import com.junchenrobot.mes.dao.WinderTaskDao;
import com.junchenrobot.mes.domain.WinderTask;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicIntegerArray;
import java.util.concurrent.locks.ReentrantLock;

@Slf4j
@Component
public class TaskExecutorCenter {
    
    private final Map<String, AtomicIntegerArray> taskMap = new ConcurrentHashMap<>(16);
    
    private final ReentrantLock lock = new ReentrantLock();
    
    private final WinderTaskDao winderTaskDao;
    
    private final DeviceDao deviceDao;
    
    private final KCClient kcClient;
    
    public TaskExecutorCenter(WinderTaskDao winderTaskDao, DeviceDao deviceDao, KCClient kcClient) {
        this.winderTaskDao = winderTaskDao;
        this.deviceDao = deviceDao;
        this.kcClient = kcClient;
    }
    
    /**
     * 添加任务
     *
     * @param taskCode 任务代码
     */
    public void addTask(String taskCode) {
        // 不存在才会添加
        if (!taskMap.containsKey(taskCode)) {
            taskMap.put(taskCode, new AtomicIntegerArray(new int[10]));
            log.info("添加键");
        }
    }
    
    
    /**
     * 删除任务
     *
     * @param taskCode 任务代码
     */
    public void removeTask(String taskCode) {
        // 移除某个元素
        taskMap.remove(taskCode);
    }
    
    public int size() {
        return taskMap.size();
    }
    
    
    /**
     * 检查异步任务执行状态
     * 0: 未创建异步任务
     * 1: 已创建异步任务
     * 2: 执行完毕异步任务
     *
     * @param taskCode 任务代码
     * @param status   状态
     * @return {@link Integer}
     */
    public Integer checkState(String taskCode, Integer status) {
        Integer result = 0;
        try {
            boolean tryLock = lock.tryLock(10, TimeUnit.SECONDS);
            if (tryLock) {
                if (status == 20) {
                    status = 7;
                }
                if (taskMap.containsKey(taskCode)) {
                    AtomicIntegerArray atomicIntegerArray = taskMap.get(taskCode);
                    return atomicIntegerArray.get(status);
                }
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            lock.unlock();
        }
        return result;
    }

    /**
     * 清空该任务
     * @param taskCode
     */
    public void clearStatus(String taskCode) {
        try {
            boolean tryLock = lock.tryLock(10, TimeUnit.SECONDS);
            if (tryLock) {
                if (taskMap.containsKey(taskCode)) {
                    AtomicIntegerArray atomicIntegerArray = taskMap.get(taskCode);
                    for (int i = 0; i < atomicIntegerArray.length(); i++) {
                        int status = atomicIntegerArray.get(i);
                        atomicIntegerArray.set(status, 0);
                    }
                }
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            lock.unlock();
        }
    }
    
    
    /**
     * 设置任务状态
     *
     * @param taskCode 任务编号
     * @param status   状态
     */
    public void setStatus(String taskCode, Integer status) {
        try {
            boolean tryLock = lock.tryLock(10, TimeUnit.SECONDS);
            if (tryLock) {
                if (status == 20) {
                    status = 7;
                }
                if (taskMap.containsKey(taskCode)) {
                    AtomicIntegerArray atomicIntegerArray = taskMap.get(taskCode);
                    atomicIntegerArray.set(status, 1);
                }
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            lock.unlock();
        }
    }
    
    
    /**
     * 查看是否够存在某键
     *
     * @param taskCode 任务编号
     * @return boolean
     */
    public boolean exist(String taskCode) {
        return taskMap.containsKey(taskCode);
    }
    
    /**
     * 更新任务完成
     *
     * @param taskCode 任务编号
     * @param status   状态
     */
    public void updateTaskFinished(String taskCode, Integer status) {
        if (taskMap.containsKey(taskCode)) {
            AtomicIntegerArray atomicIntegerArray = taskMap.get(taskCode);
            atomicIntegerArray.set(status, 2);
        }
    }
    
    /**
     * 更新任务异常
     *
     * @param taskCode 任务编号
     * @param status   状态
     */
    public void updateTaskException(String taskCode, Integer status) {
        if (taskMap.containsKey(taskCode)) {
            AtomicIntegerArray atomicIntegerArray = taskMap.get(taskCode);
            atomicIntegerArray.set(status, 3);
        }
    }

    /**
     * 删除任务
     * @param taskCode
     */
    public void cancelTask(String taskCode) {
         kcClient.deleteTask(taskCode);
    }
    
}
